package com.lovedice;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;

public class PermissionDialog extends Dialog implements OnClickListener {
	private Button okButton;
	private Button cancelButton;
	private SharedPreferences preferences;
	private CheckBox checkNotShowAgain;

	public PermissionDialog(Context context) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.permission_dialog);
		okButton = (Button) findViewById(R.id.ok_button);
		okButton.setOnClickListener(this);
		cancelButton = (Button) findViewById(R.id.cancel_button);
		cancelButton.setOnClickListener(this);
		checkNotShowAgain = (CheckBox) findViewById(R.id.check_not_show_again);
		preferences = PreferenceManager.getDefaultSharedPreferences(context);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			dismiss();
			System.exit(0);
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		if (v == okButton) {
			if(checkNotShowAgain.isChecked()) {
				Editor edit = preferences.edit();
				edit.putBoolean("show_permission_dialog", false);
				edit.commit();
			}
			dismiss();
		} else if (v == cancelButton) {
			dismiss();
			System.exit(0);
		}
	}
	
}
